package edu.pku.sei.mte.runtime.rulebased;

import edu.pku.sei.mte.mtemodel.model.MClass;
import edu.pku.sei.mte.mtemodel.mtmodel.common.Variable;
import edu.pku.sei.mte.mtemodel.mtmodel.expression.Expression;
import edu.pku.sei.mte.mtemodel.mtmodel.pattern.TPatternNode;
import edu.pku.sei.mte.runtime.Context;
import edu.pku.sei.mte.transformation.VisitorFactory;
import edu.pku.sei.mte.transformation.expression.EvaluateExpressionVisitor;

public class KeyPair {
	private String prop;
	private Object hint;
	
	private  Object cachedContext;
	private Object cachedValue = null;;
	
	public String getProp() {
		return prop;
	}
	public void setProp(String prop) {
		this.prop = prop;
	}
	
	public Object getValue(Context c) throws Exception {
		if(c==cachedContext)
			if(cachedValue!=null) return cachedValue;
		
		cachedContext = c;
		
		if(hint instanceof Expression){
			EvaluateExpressionVisitor eev = VisitorFactory.INSTANCE.getCheckOnlyExpressionVisitor((RBTContext) c, null);
			Object value = ((Expression)hint).accept(eev);
			cachedValue = value;
			return value;
		} else if(hint instanceof TPatternNode){
			Variable v = c.getVariable(((TPatternNode)hint).getName());
			if(v.isValid()){
				MClass cls = (MClass)v.getValue();
				cachedValue = cls;
				return cls;
			} else
				return null;//error creation order error
		} else
			return hint;
	}
	public void setHint(Object hint) {
		this.hint = hint;
	}
	
	

}
